package com.cby.ssm.service.impl;

import com.cby.common.PasswordUtil;
import com.cby.common.ThisSystemException;
import static com.cby.common.AssertThrowUtil.*;
import com.cby.ssm.dao.UserDao;
import com.cby.ssm.entity.UserEntity;
import com.cby.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by 13296 on 2017/7/13.
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao udao;
    @Override
    public UserEntity login(String account, String password) throws Exception {
        /*if(account==null||account.trim().length()==0){
            throw new ThisSystemException("账户不能为空");
        }*/
        /*
        account = ThisSystemUtil.throwIfBlank("账户不能为空！",account);
        password = ThisSystemUtil.throwIfBlank("密码不能为空！",password);*/
        account =$("账户不能为空！",account);
        password = $("密码不能为空！",password);

        UserEntity u = udao.select("account",account);
        if (u==null){
            throw new ThisSystemException("账户不存在！");
        }
        if(!PasswordUtil.verify(password,u.getPassword())){
            throw new ThisSystemException("密码错误！");
        }
        return u;
    }


    @Override
    public List<UserEntity> searchAllUser() throws Exception {
        List<UserEntity> list = udao.selectAllUser();
        return list;
    }

    @Override
    public UserEntity searchUser(String id) throws Exception {
        id = $("id不能为空",id);
        UserEntity u = udao.select("id",id);
        return u;
    }

    public List<UserEntity>  searchUsers(String searchKey) throws Exception {
        searchKey = $("请输入搜索的内容",searchKey);
        List<UserEntity> list = udao.selectLike(searchKey);
        return list;
    }
    @Override
    public UserEntity editUser(UserEntity u) throws Exception {
        udao.update(u);
        //System.out.println("CHANGE: "+u.getId());
        return u;
    }

    @Override
    public boolean deleteUser(String deleteId,String notDeleteId) throws Exception {
        assertNotEquals("notDelete",deleteId,notDeleteId);
        udao.delete("id",deleteId);
        return false;
    }

    @Override
    public UserEntity updatePassword(String id,String oldPassword, String newPassword, String newPasswordConfirm) throws Exception {
        //1.验证参数
        oldPassword = $("旧密码必须填写！",oldPassword);
        newPassword = $("新密码必须填写！",newPassword);
        newPasswordConfirm = $("新密码确认必须填写！",newPasswordConfirm);
        assertEquals("两次密码不一致",newPassword,newPasswordConfirm);
        assertNotEquals("新密码和旧密码不能一样",oldPassword,newPassword);
        //2.找到用户
        UserEntity u = udao.select("id",id);
        /*
        if(id==null){
            throw  new ThisSystemException("无法找到这个用户！");
        }*/
        assertNotNull("无法找到这个用户",u);

        //3/验证旧密码
        /*if(!u.getPassword().equals(oldPassword)){
            throw new ThisSystemException("旧密码不正确！");
        }*/
        if(!PasswordUtil.verify(oldPassword,u.getPassword())){
            throw new ThisSystemException("旧密码错误！");
        }
        //assertEquals("旧密码不正确",u.getPassword(),oldPassword);
        //4.更新密码
        newPassword = PasswordUtil.generate(newPassword);
        u.setPassword(newPassword);

        udao.update(u);

        return u;
    }

    @Override
    public void addUser(String account, String passwordAgain,String password, String name, String sex, String telephone, String email, String address) throws Exception {
        //验证ID、账户、密码、名字不能为空
        account =$("账户不能为空！",account);
        password = $("密码不能为空！",password);
        passwordAgain = $("确认密码不能为空！",passwordAgain);
        name=$("名字不能为空",name);
        assertEquals("密码和确认密码不一样",password,passwordAgain);
        //账户唯一性
        List<UserEntity> list = new ArrayList<>();
        list.addAll(udao.selectAllUser());
        for(UserEntity u:list){
            assertNotEquals("该账号已存在！",u.getAccount(),account);
        }
//        for(int i=0;i<100;i++){
//            password = PasswordUtil.generate(password);
//            UserEntity u = new UserEntity(account+i,password,name+i,sex,telephone,email,address,new Date());
//            udao.insert(u);
//        }
        password = PasswordUtil.generate(password);
        UserEntity u = new UserEntity(account,password,name,sex,telephone,email,address,new Date());
        udao.insert(u);
    }
}
